<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				sponsorIdCacheKey:'zyk-hl-saneProposal-sponsor-cache',
				fileKey:'53'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				_that.formdata.updateBottomFileFlag= true
				if(Object.prototype.toString.call(_that.$refs.autoform.formdata.bottomFileList) == "[object String]"){
					_that.formdata.bottomFileListStr = _that.$refs.autoform.formdata.bottomFileList|| ''
				}else{
					_that.formdata.bottomFileListStr = JSON.stringify(_that.$refs.autoform.formdata.bottomFileList || []) || ''
					
				}
				delete _that.formdata.bottomFileUpload
				delete _that.formdata.bottomFileList
				if(_that.edit){
					_that.om.formSave("saneProposal/doUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("saneProposal/doAdd",_that.formdata,_that)
				}  
			},
			onSelectPost(item){
				let _that = this
				_that.$refs.autoform.formdata.organizationId = item.instId || ''
				_that.$refs.autoform.formdata.organizationName = item.instName || ''
			},
			getPhone(id){
				let _that = this
				_that.om.request(
					"saneProposal/getPhone",
					{
						UserId:'id'
					}
				).then(res=>{
					_that.$refs.autofrom.formdata.phone = res.value
				})
			},
			findPostList(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getPosComboboxByUser",
					{
						userId:id,
						flag:true
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("postTitle","postId","data",configSelectData(res,"id","posName"))
					_that.$refs.autoform.initSelectVal("postTitle","postId")
				})
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					urlUtil.sysdictComboboxUrl('39176'),
					{
						url:'saneProposal/getPhone',
						param:{
							UserId:_that.userId
						}
					},{
						url:'saneProposal/getCode'
					}  
				] 
				if(fileId){
					_that.editId = fileId
					ajaxArr.push({
						url:"saneProposal/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posName"),mainData)
					changeEditor("postTitle","postId","data",configSelectData(res[0],"id","posName"),mainData)
					changeEditor("postTitle","proposalCategoryId","data",configSelectData(res[1],"id","dicname"),mainData)
					changeEditor("postTitle","sponsorId","isSearchUser",true,mainData)
					changeEditor("postTitle","sponsorId",{
						link:'../../public/searchUser/searchUser',
						"linkParam":{
							cachename: _that.sponsorIdCacheKey,
							isSingle:true
						}
					},null,mainData)
					let tempFileList = []
					let fileUrlList = []
					changeEditor('bottominfo','bottomFileUpload','fileUploadKey',"53",mainData)
					changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
					changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
					
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[4].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[4].value[index])
								_that.$set(_that.formdata,index,res[4].value[index])
							}
						}
						if(_that.formdata.bottomFileList){
							let tempFileList = []
							let fileUrlList = _that.formdata.bottomFileList.map(t=>{
								tempFileList.push(t.fileSaveName)
								return t.fileSaveName
							})
							changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
							changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
						}
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.sponsorCode){
							_that.$refs.autoform.formdata.sponsorCode = res[3].value
						}
						if(!_that.$refs.autoform.formdata.phone){
							_that.$refs.autoform.formdata.phone = res[2].value
						}
						if(!_that.$refs.autoform.formdata.sponsorId){
							_that.$refs.autoform.formdata.sponsorId = _that.userId
						}
						if(!_that.$refs.autoform.formdata.sponsorName){
							_that.$refs.autoform.formdata.sponsorName = _that.userName
						}
						if(!_that.$refs.autoform.formdata.postId){
							_that.$refs.autoform.initSelectVal("postTitle","postId")
						}else{
							_that.$refs.autoform.initSelectIndex("postTitle","postId")
						}
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
						_that.$refs.autoform.initSelectIndex("postTitle","proposalCategoryId")
					})
				},options)
			}
		},
		onLoad(options) { 
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.sponsorIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.sponsorId = d.id
							formdata.sponsorName = d.username
							_that.findPostList(d.id)
							_that.getPhone(d.id)
							uni.removeStorage({
								key:_that.sponsorIdCacheKey
							})
						}
					}
				})
			}
			
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
